home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2774 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Floating point calculation order
  5. Date: Tue, 23 Jan 96 20:13:58 GMT
  6. Organization: none
  7. Message-ID: <822428038snz@genesis.demon.co.uk>
  8. References: <m0tedv8-0002eqC@sice.nsk.su> <3104c6d9.134061184@nntp.ix.netcom.com> <DLnE5K.2xH@microunity.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <DLnE5K.2xH@microunity.com> toms@MicroUnity.com "Tom Sanders" writes:
  15.  
  16. >In article <3104c6d9.134061184@nntp.ix.netcom.com>, miker3@ix.netcom.com (Mike
  17. > Rubenstein) writes:
  18. >|> "Pavel A. Zemtsov" <PZEM@sice.nsk.su> wrote:
  19. >|> 
  20. >|> > 
  21. >|> >  Having declaration
  22. >|> > 
  23. >|> >  double x, p, q, r;
  24. >|> > 
  25. >|> > my compiler (cc on SCO 3.2) calculated following expression:
  26. >|> > 
  27. >|> >  x = p * q / r;
  28. >|> > 
  29. >|> > as p * (q/r); (I mean, it divided first, than multiplied). That 
  30. >|> > resulted in precision loss.
  31. >|> > 
  32. >|> > Is this legal behavior?
  33. >|> 
  34. >|> Not in ANSI C, but it was legal in K&R. If memory serves, it was not
  35. >|> made illegal until very late in the standardizattion process so if
  36. >|> your compiler was written before the standard was finalized it may not
  37. >|> adhere to this.
  38. >|> 
  39. >|> Michael M Rubenstein
  40. >
  41. >I am a firm believer in not allowing a compiler to make these decisions for
  42. >me.  It's best to specify with parens the order of evaluation and not leave
  43. >it up to the compiler defaults.  I would actually prefer a compiler that
  44. >gave a warning if I did have an equation that could be evaluated in more
  45. >than 1 order (something I have not done in over 20 years).
  46.  
  47. Parentheses in C allow you to change the operator/operand grouping within
  48. expressions. They don't enforce any order of evaluation beyond that implied
  49. by the grouping. So:
  50.  
  51.  x = p * q / r;
  52.  
  53. and
  54.  
  55.  x = (p * q) / r;
  56.  
  57. mean *exactly* the same thing in C since the parentheses don't change the
  58. grouping.
  59.  
  60. -- 
  61. -----------------------------------------
  62. Lawrence Kirby | fred@genesis.demon.co.uk
  63. Wilts, England | 70734.126@compuserve.com
  64. -----------------------------------------
  65.